import 'dart:math';

import 'package:fijkplayer/fijkplayer.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:industry_net/base/common/ijkplayer/iplayer.dart';
import 'package:industry_net/base/common/util.dart';
import 'package:industry_net/base/component/list/section.dart';
import 'package:industry_net/pages/news/main/models/comment.dart';
import 'package:industry_net/pages/news/main/models/news.dart';
import 'package:industry_net/pages/news/news_reply/index.dart';
import 'widgets/cell.dart';

import 'index.dart';
import 'widgets/comment_bar/controller.dart';

class NewsDetailController extends SuperController {
  NewsDetailController();
  final controller = CommentController();
  final state = NewsDetailState();

  void getData() {
    var _items = List.generate(
        10,
        (index) => NewsCommentModel()
          ..isPraised = index ~/ 3 == 0
          ..replys = List.generate(
              Random().nextInt(10), (index) => NewsCommentModel()));
    state.data = [
      SectionModel(
          index: 0,
          itemBuilder: (context, index) {
            var _md = _items[index];
            return NewsCommentCell(md: _md);
          },
          items: _items)
    ];
  }

  void actionReply(GlobalKey key, NewsCommentModel md) async {
    if (controller.state.focusNode.hasFocus) {
      controller.state.focusNode.unfocus();
      return;
    }

    // var res = await Get.toNamed(RouterPath.news_reply, arguments: md);
    // if (res == true) {
    //   getData();
    //   update();
    // }

    showReplyPage(md: md);
  }

  void actionNewsPraise(GlobalKey key) async {
    state.md.isPraised = !state.md.isPraised;
    state.md.isAnimating = true;
    update();
    await Future.delayed(const Duration(milliseconds: 300));
    state.md.isAnimating = false;
  }

  void actionCommentPraise(NewsCommentModel md) async {
    md.isPraised = !md.isPraised;
    md.isAnimating = true;
    update();
    await Future.delayed(const Duration(milliseconds: 300));
    md.isAnimating = false;
  }

  void _initData() {
    state.md = Get.arguments;
  }

  void _initViewState() {
    if (state.md.type == NewsType.video) {
      IPlayer.instance.play(
          'https://sample-videos.com/video123/flv/240/big_buck_bunny_240p_10mb.flv',
          autoPlay: true);
    }
  }

  void actionShare() {
    showShare();
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    _initData();
    getData();
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
    _initViewState();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }

  @override
  void onDetached() {}

  @override
  void onInactive() {}

  @override
  void onPaused() {
    if (state.md.type == NewsType.video) {
      if (IPlayer.instance.player.state == FijkState.started) {
        state.isEnterBackgroundPause = true;
        IPlayer.instance.pause();
      } else {
        if (IPlayer.instance.player.state == FijkState.initialized ||
            IPlayer.instance.player.state == FijkState.asyncPreparing) {
          state.isEnterBackgroundReset = true;
          IPlayer.instance.reset();
        }
      }
    }
  }

  @override
  void onResumed() {
    if (state.isEnterBackgroundPause) {
      state.isEnterBackgroundPause = false;
      IPlayer.instance.start();
    } else if (state.isEnterBackgroundReset) {
      IPlayer.instance.play(
          'https://sample-videos.com/video123/flv/240/big_buck_bunny_240p_10mb.flv',
          autoPlay: true);
    }
  }
}
